package com.scalar.forecast.forecastSetup.actionCommands;

import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.forecastSetup.forms.ForecastSetupForm;
import com.scalar.forecast.forecastSetup.dao.ForecastSetupDAO;
import com.scalar.forecast.forecastSetup.ForecastEntryField;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

/**
 * User: Sujan Kumar
 * Date: Feb 10, 2005
 * Time: 10:13:27 AM
 */
public class ForecastSetupReloadForecastEntryFieldsCommand extends ActionCommand
{
    public ActionForward execute ( ActionMapping mapping,
                                            ActionForm form,
                                            HttpServletRequest request,
                                            HttpServletResponse response )
    {
        ForecastSetupForm forecastSetupForm = ( ForecastSetupForm ) form;
        ArrayList oldEntryFields = forecastSetupForm.getForecastEntryFields();
        ArrayList newEntryFields = ForecastSetupDAO.getForecastEntryFields( null );
        int oldEntryFieldsSize = oldEntryFields == null ? 0 : oldEntryFields.size();
        int newEntryFieldsSize = newEntryFields == null ? 0 : newEntryFields.size();
        ForecastEntryField oldEntryField = null, newEntryField = null;
        for ( int i = 0; i < oldEntryFieldsSize; i++ )
        {
            oldEntryField = ( ForecastEntryField )oldEntryFields.get ( i );
            for ( int j = 0; j < newEntryFieldsSize; j++ )
            {
                newEntryField = ( ForecastEntryField )newEntryFields.get ( j );
                if ( oldEntryField.getRowId() == newEntryField.getRowId() )
                {
                    newEntryField.setEntrySelected( oldEntryField.isEntrySelected() );
                    break;
                }
            }
        }
        forecastSetupForm.setForecastEntryFields( newEntryFields );
        ActionForward actionForward = mapping.findForward ( "forecastSetup" );
        return actionForward;
    }
}
